package com.speciality.demo.demos.service.impl;

import com.speciality.demo.demos.dto.ChartDataVO;
import com.speciality.demo.demos.dto.DisasterStatDTO;
import com.speciality.demo.demos.dto.HeatMapVO;
import com.speciality.demo.demos.entity.WeatherHourly;
import com.speciality.demo.demos.mapper.WeatherHourlyMapper;
import com.speciality.demo.demos.service.WeatherChartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class WeatherChartServiceImpl implements WeatherChartService {

    @Autowired
    private WeatherHourlyMapper weatherHourlyMapper;

    // 硬编码哈尔滨经纬度（固定值）
    private static final double HARBIN_LON = 126.63;
    private static final double HARBIN_LAT = 45.75;

    @Override
    public ChartDataVO getClimateTrendData(LocalDateTime startTime, LocalDateTime endTime) {
        List<WeatherHourly> dataList = weatherHourlyMapper.selectClimateTrendByTimeRange(startTime, endTime);

        List<String> timeAxis = new ArrayList<>();
        List<Double> tempData = new ArrayList<>();
        List<Integer> humidityData = new ArrayList<>();
        List<Double> windSpeedData = new ArrayList<>();

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
        for (WeatherHourly item : dataList) {
            timeAxis.add(item.getTime());
            System.out.println("存入的格式"+timeAxis);
            tempData.add(item.getTemperature2m());      // 对应数据库 `temperature_2m`
            humidityData.add(item.getRelativehumidity2m()); // 对应 `relativehumidity_2m`
            windSpeedData.add(item.getWindspeed10m());   // 对应 `windspeed_10m`
        }

        ChartDataVO vo = new ChartDataVO();
        vo.setXAxis(timeAxis);
        vo.setTemperatureData(tempData);
        vo.setHumidityData(humidityData);
        vo.setWindSpeedData(windSpeedData);
        return vo;
    }

    @Override
    public HeatMapVO getDisasterHeatData(LocalDateTime startTime, LocalDateTime endTime) {
        List<DisasterStatDTO> statList = weatherHourlyMapper.selectDisasterByTimeRange(startTime, endTime);
        System.out.println(statList);
        List<List<Number>> heatData = new ArrayList<>();
        for (DisasterStatDTO stat : statList) {
            heatData.add(Arrays.asList(
                    HARBIN_LON,          // 固定经度
                    HARBIN_LAT,          // 固定纬度
                    calculateWeight(stat.getWeathercode())// 灾害权重

            ));
        }

        HeatMapVO vo = new HeatMapVO();
        vo.setHeatData(heatData);

        // 天气代码→名称映射（前端Tooltip用）
        Map<String, String> codeMap = new HashMap<>();
        codeMap.put("45", "雾");
        codeMap.put("48", "冻雾");
        codeMap.put("65", "大雨");
        codeMap.put("66", "轻度冻雨");
        codeMap.put("67", "重度冻雨");
        codeMap.put("75", "大雪");
        codeMap.put("82", "强阵雨");
        codeMap.put("86", "中至重度阵雪");
        codeMap.put("95", "雷雨（无冰雹）");
        codeMap.put("96", "雷雨+冰雹");
        vo.setRegionMap(codeMap); // 复用字段存储代码映射

        return vo;
    }

    // 灾害权重计算（根据代码严重程度）
    private int calculateWeight(int weathercode) {
        switch (weathercode) {
            case 45: return 1;
            case 48: return 2;
            case 65: return 3;
            case 66: return 2;
            case 67: return 4;
            case 75: return 3;
            case 82: return 3;
            case 86: return 3;
            case 95: return 2;
            case 96: return 5;
            default: return 0;
        }
    }




}
